package com.kefu.robot.controller;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.kefu.common.annotation.Permission;
import com.kefu.common.context.KefuContext;
import com.kefu.common.controller.AbstractBaseController;
import com.kefu.common.result.Result;
import com.kefu.common.result.ResultGenerator;
import com.kefu.common.vo.DeleteVo;
import com.kefu.common.vo.DetailVo;
import com.kefu.robot.model.RobotSynonym;
import com.kefu.robot.service.RobotSynonymService;
import com.kefu.robot.vo.RobotSynonymInsertVo;
import com.kefu.robot.vo.RobotSynonymSearchVo;
import com.kefu.robot.vo.RobotSynonymUpdateVo;

import cn.hutool.core.util.StrUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;


@Api(tags="同义词")
@RestController
@RequestMapping("robot/synonym")
public class RobotSynonymController extends AbstractBaseController{

    @Autowired
    private RobotSynonymService robotSynonymService;

    @PostMapping("/add")
    @ApiOperation(value="新增同义词", notes="新增同义词")
    public Result<String> add(@Validated @RequestBody RobotSynonymInsertVo robotSynonymVo) {
    	logger.info("新增同义词:{}",robotSynonymVo);

    	if(StrUtil.hasBlank(robotSynonymVo.getTitle(),robotSynonymVo.getQuestion())){
            return ResultGenerator.genValidFailResult();
        }
    	//中文逗号替换为英文逗号
    	RobotSynonym info=robotSynonymVo.convert();
        info.setTitle(robotSynonymVo.getTitle().replaceAll("，",","));
        info.setQuestion(robotSynonymVo.getQuestion().replaceAll("，",","));
        robotSynonymService.save(info);
        return ResultGenerator.genSuccessResult(info.getId());
    }

    @PostMapping("/delete")
    @ApiOperation(value="删除同义词", notes="删除同义词")
    public Result<String> delete(@Validated @RequestBody DeleteVo delete) {
    	logger.info("删除同义词:{}",delete);
    	
        robotSynonymService.deleteById(delete.getId());
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    @ApiOperation(value="修改同义词", notes="修改同义词")
    public Result<String> update(@Validated @RequestBody RobotSynonymUpdateVo robotSynonymVo) {
    	logger.info("修改同义词:{}",robotSynonymVo);
        if(StrUtil.hasBlank(robotSynonymVo.getTitle(),robotSynonymVo.getQuestion())){
            return ResultGenerator.genValidFailResult();
        }
        //中文逗号替换为英文逗号
    	RobotSynonym info=robotSynonymVo.convert();
        info.setTitle(robotSynonymVo.getTitle().replaceAll("，",","));
        info.setQuestion(robotSynonymVo.getQuestion().replaceAll("，",","));
        robotSynonymService.update(info);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    @ApiOperation(value="同义词详情", notes="同义词详情")
    public Result<RobotSynonym> detail(@Validated @RequestBody DetailVo detail) {
        logger.info("同义词查看详情:{}",detail);
        
        RobotSynonym robotSynonym = robotSynonymService.findById(detail.getId());
        return ResultGenerator.genSuccessResult(robotSynonym);
    }

    @PostMapping("/list")
    @ApiOperation(value="查询同义词", notes="查询同义词")
    public Result<Page<RobotSynonym>> list(@Validated @RequestBody RobotSynonymSearchVo search) {    
        logger.info("查询同义词:{}",search);
        
        Page<RobotSynonym> list = robotSynonymService.search(search);
        return ResultGenerator.genSuccessResult(list);
    }
}
